<?php
include('../shared/ezsql/ez_sql_mysql.php');
include('../model/user.class.php');
include('../model/user.say.class.php');


/**
 * 用户操作类 ，登陆、注册等
 */
class UO{
	private static $DS = "182.18.22.139:3306";
	private static $DU = "jacker0";
	private static $DP = "ljp0213";
	private static $DN = "cdevnotes";

	/**
	 * 用户登陆
	 * @param $user 用户信息
	 */
	public static function login(User $user){

		$sql_login = "SELECT COUNT(*) FROM logininfo WHERE loginName='$user->loginName' AND pwd='$user->password';";

		$DB = new ezSQL_mysql(self::$DU,self::$DP,self::$DN,self::$DS);

		$login_result = $DB->get_var($sql_login);

		return $login_result > 0 ? 1 : 0;

	}

	/**
	 * 检查是否存在此用户
	 * @param $login_name 登陆名
	 */
	private static function check_user_exists($login_name){
		$sql_login = "SELECT COUNT(*) FROM logininfo WHERE loginName='$login_name';";

		$DB = new ezSQL_mysql(self::$DU,self::$DP,self::$DN,self::$DS);
		$check_user_result = $DB->get_var($sql_login);

		return $check_user_result > 0 ? true : false;
	}

	/**
	 * 用户注册
	 * @param User $user 用户资料
	 */
	public static function reigster(User $user){
		if(!self::check_user_exists($user->loginName)){
				
			$insert_logininfo_sql = "INSERT INTO logininfo(uid,loginName,pwd,registerDate,lastUpdateDate) VALUES('$user->uid','$user->loginName','$user->password','$user->regDate','$user->lastUpdDate');";
			$insert_userinfo_sql = "INSERT INTO userinfo(uid,userName,nickName,sex,tel,email,address) VALUES('$user->uid','$user->userName','$user->nickName','$user->sex','$user->tel','$user->email','$user->address');";

			$DB = new ezSQL_mysql(self::$DU,self::$DP,self::$DN,self::$DS);
			$DB->query("set names utf8");
			$transaction_result = $DB->transaction(array($insert_logininfo_sql, $insert_userinfo_sql));
			return ($transaction_result) ? 1 : 0;
		} else {
			return -1;
		}
	}
	
	/**
	 * 获取用户信息
	 * @param User $user
	 * @param $by
	 */
	public static function getUserInfo(User $user, $by){
		$sql_get_user = "SELECT ui.uid,li.loginName,ui.nickName,ui.sex,ui.tel,ui.email,ui.address"
		." FROM userinfo ui LEFT JOIN  logininfo li ON ui.uid=li.uid "
		." WHERE ";
		if($by == "uid"){
			$sql_get_user =  $sql_get_user." ui.uid='$user->uid'";
		} elseif ($by == "loginInfo") {
			$sql_get_user =  $sql_get_user." li.loginName='$user->loginName' AND li.pwd='$user->password';";
		}
		$DB = new ezSQL_mysql(self::$DU,self::$DP,self::$DN,self::$DS);

		$row = $DB->get_row($sql_get_user);

		$user->uid = $row->uid;
		$user->loginName = $row->loginName;
		$user->nickName = $row->nickName;
		$user->sex = ($row->sex == '男') ? 1 : 2 ;
		$user->tel = $row->tel;
		$user->email = $row->email;
		$user->address = $row->address;

		return $user;
	}

	//$id,$uid,$content,$publishDate,$commonNum,$viewNum
	public static function publishSay(Say $userSay){
		$insert_say_sql = "INSERT INTO userSay(uid,content,publishDate) VALUES('$userSay->uid','$userSay->content','$userSay->publishDate');";
		$DB = new ezSQL_mysql(self::$DU,self::$DP,self::$DN,self::$DS);
		$say_result = $DB->query($insert_say_sql);
		return ($say_result > 0) ? 1 : 0 ;
	}

	public static function comment($uid, $comid, $content){

	}
	//$id,$reid,$type,$uid,$content,$reDate
	public static function reply(Reply $reply){
		$insert_say_sql = "INSERT INTO userReply(reid,type,content,uid,reDate) VALUES('$reply->reid',$reply->type,'$reply->content','$reply->uid','$reply->reDate')";
		$DB = new ezSQL_mysql(self::$DU,self::$DP,self::$DN,self::$DS);
		$say_result = $DB->query($insert_say_sql);
		return ($say_result > 0) ? 1 : 0 ;
	}
}

?>
